<template>
  <div class="audit-factory">
    <div class="audit-box" :class="{ 'factory-import': isImport }">
      <tag-factory-result :derivedTag="auditDetail.deriveTagResult" isImport v-if="isImport"></tag-factory-result>
      <template v-else>
        <div class="audit-title linear-cyan">
          <span class="icon"><icon-svg name="iconFunc"></icon-svg></span><br />
          客群逻辑
        </div>
        <div class="audit-content audit-text tag-scrollbar">
          <tag-factory-logic :derivedTag="derivedTag" :drillGrps="drillGrps"></tag-factory-logic>
        </div>
      </template>
    </div>

    <div class="audit-box">
      <div class="audit-title linear-purpose">
        <span class="icon"><icon-svg name="iconFunc"></icon-svg></span><br />
        客群画像
      </div>
      <div class="audit-content audit-text grpflter-content tag-scrollbar">
        <tag-factory-grpflter ref="tagFactoryGrpflter" :grpFlter="grpFlter" :grpTotal="grpTotal"></tag-factory-grpflter>
      </div>
    </div>
  </div>
</template>

<script>
import factory from '@/views/modules/factory/factory'
export default {
  props: {
    auditDetail: {
      type: Object,
      required: true,
      default: () => {}
    }
  },
  computed: {
    derivedTag() {
      let derivedTag = {
        logicalOperation: 'and',
        tagList: []
      }
      if (this.auditDetail.deriveTagResult) {
        const result = JSON.parse(this.auditDetail.derivedTag.derivedTag)
        let list = result.request.labGrps.labs ? result.request.labGrps.labs : []
        derivedTag.logicalOperation = result.request.labGrps.logicTyp
        derivedTag.tagList = factory.initLogic(list, this.auditDetail.baseTags)
      }
      return derivedTag
    },
    drillGrps() {
      let drillGrps = []
      if (this.auditDetail.deriveTagResult) {
        let dataJson = JSON.parse(this.auditDetail.derivedTag.derivedTag)
        drillGrps = dataJson.request.drillGrps
      }
      return drillGrps
    },
    grpFlter() {
      let grpFlter = []
      if (this.auditDetail.deriveTagResult) {
        let dataJson = JSON.parse(this.auditDetail.derivedTag.derivedTag)
        dataJson.request.grpFlter.forEach(item => {
          let filterTag = this.auditDetail.baseTags.find(tag => tag.assetCode === item.mainGrp.labId)
          let params = {
            grpId: item.grpId,
            mainGrp: {
              labId: item.mainGrp.labId,
              groupTyp: item.mainGrp.groupTyp,
              labGrps: item.mainGrp.labGrps,
              groupName: dataJson.extra[item.grpId][item.mainGrp.labId].groupName,
              graphType: dataJson.extra[item.grpId][item.mainGrp.labId].graphType,
              labTbl: filterTag.labTbl,
              labFldMap: filterTag.labFldMap,
              rangeInfo: dataJson.extra[item.grpId][item.mainGrp.labId].rangeInfo,
              tagNm: filterTag.assetNm,
              valueType: filterTag.valueType,
              enums: filterTag.enums
            }
          }
          if (item.subGrp && item.subGrp.labId) {
            let filterSubTag = this.auditDetail.baseTags.find(tag => tag.assetCode === item.subGrp.labId)
            params.subGrp = {
              labId: item.subGrp.labId,
              groupTyp: item.subGrp.groupTyp,
              labGrps: item.subGrp.labGrps,
              groupName: dataJson.extra[item.grpId][item.subGrp.labId].groupName,
              graphType: dataJson.extra[item.grpId][item.subGrp.labId].graphType,
              labTbl: filterSubTag.labTbl,
              labFldMap: filterSubTag.labFldMap,
              rangeInfo: dataJson.extra[item.grpId][item.subGrp.labId].rangeInfo,
              tagNm: filterSubTag.assetNm,
              valueType: filterSubTag.valueType,
              enums: filterSubTag.enums
            }
          }
          grpFlter.push(params)
        })
      }
      return grpFlter
    },
    grpTotal() {
      return this.auditDetail.deriveTagResult ? JSON.parse(this.auditDetail.deriveTagResult).grpTotal : 0
    },
    isImport() {
      return this.auditDetail.derivedTag ? JSON.parse(this.auditDetail.derivedTag.derivedTag).request.custGroupId : ''
    }
  },
  data() {
    return {}
  },
  methods: {
    refreshGrpflter() {
      this.$refs.tagFactoryGrpflter.refresh(JSON.parse(this.auditDetail.deriveTagResult))
    }
  }
}
</script>

<style lang="scss">
.audit-factory {
  .audit-box {
    height: 250px !important;
    &.factory-import {
      height: 180px !important;
      .story-box {
        margin-top: 0;
      }
      .icon {
        margin-top: 40px !important;
      }
    }
  }
  .icon {
    margin-top: 60px !important;
  }
  .logic-wrap {
    height: 100%;
  }
  .grpflter-content .tag-factory-grpflter {
    padding: 10px;
    background-color: #f6f7f8;
  }
}
</style>
